package com.rongxun.hbwallet.mvp.presenter;

import android.content.Context;
import android.support.annotation.NonNull;

import com.rongxun.base.BaseObserver;
import com.rongxun.base.BasePresenter;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.SPKey;
import com.rongxun.hbwallet.model.bean.ImageCode;
import com.rongxun.hbwallet.model.entity.PostCodeLoginInfo;
import com.rongxun.hbwallet.model.entity.PostMobileInfo;
import com.rongxun.hbwallet.model.bean.User;
import com.rongxun.hbwallet.mvp.contract.ICodeLogin;
import com.rongxun.utils.JsonUtils;
import com.rongxun.utils.MyToast;
import com.rongxun.utils.PreferenceUtils;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/13 10:01
 * 描述
 */
public class ICodeLoginPresenter extends BasePresenter<ICodeLogin.IView,ICodeLogin.IModel> implements ICodeLogin.IPresenter {
    public ICodeLoginPresenter(@NonNull ICodeLogin.IView pUi,ICodeLogin.IModel pModel) {
        super(pUi,pModel);
    }
    @Override
    public void release() {

    }
    @Override
    public void login(String phone, String mode, String password) {
        PostCodeLoginInfo info=new PostCodeLoginInfo();
        info.setMobile(phone);
        info.setMode(mode);
        info.setPassword(password);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), JsonUtils.toJson(info));
        addSubscribe(getModel().Login(requestBody).subscribeWith(new BaseObserver<User>(){
            @Override
            public void onSuccess(User user, Context context) {
                String hint=getView().getContext().getString(R.string.login_success);
                MyToast.showToast(getView().getContext(),hint);
                saveUserInfo(user);
            }}
        ));
    }
    @Override
    public void getCode() {
        addSubscribe(getModel().checkImgVerificationCode().subscribeWith(new BaseObserver<ImageCode>(){
            @Override
            public void onSuccess(ImageCode code, Context context) {
                 getView().showCodeimage(code);
            }}
        ));
    }

    @Override
    public void getMobileNumber(String captchaCode, String captchaId, String country, String mobile) {
        if(captchaCode.isEmpty()||mobile.isEmpty()){
            getView().showError(getView().getContext().getString(R.string.input_mobile_code),"");
        }
        PostMobileInfo info=new PostMobileInfo();
        info.setCaptchaCode(captchaCode);
        info.setCaptchaId(captchaId);
        info.setCountry(country);
        info.setMobile(mobile);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), JsonUtils.toJson(info));
        addSubscribe(getModel().getMobileCode(requestBody).subscribeWith(new BaseObserver<User>(getView()){
            @Override
            public void onSuccess(User code, Context context) {
                String hint=getView().getContext().getString(R.string.successfully);
                MyToast.showToast(getView().getContext(),hint);
            }}
        ));
    }
    @Override
    public void saveUserInfo(User user) {
        if(user!=null){
            String token = user.getToken();
            PreferenceUtils.put(SPKey.USER, JsonUtils.toJson(user));
            PreferenceUtils.put(SPKey.TOKEN, token);
            PreferenceUtils.put(SPKey.USERID, user.getUserInfo().getId());
            getView().showLogin();
        }
    }
}
